// pages/ucenter/personcenter/editdetails/createcontract/contractdetails/contractdetails.js
const util = require('../../../../../../../utils/util.js');
const api = require('../../../../../../../config/api.js');
const user = require('../../../../../../../services/user.js');

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contractid:'',//契约id
    startDay:'',//开始时间
    endDay:'',//结束时间
    isday:0,//至少打卡天数
    price:0,//每人金额
    myFormat: ['天', '时', '分', '秒'],//倒时器
    targetTime: 0,
    clearTimer:false,
    times:0,//剩余毫秒数
    posterImageUrl:'',//背景图片
    postersIp:''//背景图片ip
  },
  //倒时器回调监听
  myLinsterner(e) {
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var this_=this;
    this_.setData({
      contractid:options.contractid,
      targetTime: options.targetTime
    })
    this_.getDetailsData();
  },
  getDetailsData:function(){
    var this_ = this;
    let param = {};
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        let url = api.Interest_getContractDetails;
        param.access_token = res.access_token;
        param.id = this_.data.contractid;
        util.post(url, param).then(function (res2) {
          if (res2.code == 200) {
            if (null != res2.result.price && '' != res2.result.price){
              this_.setData({
                price: res2.result.price
              })
            }
            if (null != res2.result.validityStartValue && '' != res2.result.validityStartValue){
              this_.setData({
                startDay: res2.result.validityStartValue
              })
            }
            if (null != res2.result.validityEndValue && '' != res2.result.validityEndValue){
              this_.setData({
                endDay: res2.result.validityEndValue
              })
            }
            if (null != res2.result.validityStart && '' != res2.result.validityStart && null != res2.result.validityEnd && '' != res2.result.validityEnd){
              var end_date = new Date(res2.result.validityEnd.replace(/-/g, "/"));
              var start_date = new Date(res2.result.validityStart.replace(/-/g, "/"));
              var days = end_date.getTime() - start_date.getTime();
              var day = parseInt(days / (1000 * 60 * 60 * 24));
              this_.setData({
                isday: day
              })
            }
            if (null != res2.result.posterImageUrl && '' != res2.result.posterImageUrl){
              this_.setData({
                posterImageUrl: res2.result.posterImageUrl
              })
            }
            if (null != res2.result.postersIp && '' != res2.result.postersIp){
              this_.setData({
                postersIp: res2.result.postersIp
              })
            }
            /*if (null != res2.result.createdate){
              var thistime = util.formatTime(new Date()).replace(/-/g, "/");
              var createDate = res2.result.createdate.replace(/-/g, "/");
              var time = parseInt(new Date(thistime) - new Date(createDate));
              var targetTimes = 86400000 - time;
              if (targetTimes>0){
                var thistimes = new Date().getTime() + targetTimes
                this_.setData({
                  targetTime: thistimes
                })
              }
            }*/
          }
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
})